<?php
/**
 * @author ANHTAM
 */
class Danhmuc_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->menuDanhMuc = true;
    	Session::Remove("danhmucid");
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_title_list = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbDanhMuc = new DanhMuc();
		
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
	        
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$dataSanpham = $ojbDanhMuc->getByPrimaryKey($dataPost['itemID']);
	        	$ojbDanhMuc->delete(array('id' => $dataPost['itemID']));
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	    				$dataSanpham = $ojbDanhMuc->getByPrimaryKey($aP);
	        			$ojbDanhMuc->delete(array('id' => $aP));
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbDanhMuc->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq)));
	        	$idMin = $ojbDanhMuc->getIdBySeq($seqMin);
	        	
	        	$ojbDanhMuc->setAttribute('seq', $seqMin);
	        	$ojbDanhMuc->update(array('id' => $id));
	        	$ojbDanhMuc = new DanhMuc();
	        	$ojbDanhMuc->setAttribute('seq', $seq);
	        	$ojbDanhMuc->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbDanhMuc->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq)), true);
	        	$idMax = $ojbDanhMuc->getIdBySeq($seqMax);
	        	
	        	$ojbDanhMuc->setAttribute('seq', $seqMax);
	        	$ojbDanhMuc->update(array('id' => $id));
	        	$ojbDanhMuc = new DanhMuc();
	        	$ojbDanhMuc->setAttribute('seq', $seq);
	        	$ojbDanhMuc->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbDanhMuc->getMinMax(array(), true);
        $this->minseq = $ojbDanhMuc->getMinMax(array());
		$this->arrContent = $ojbDanhMuc->getAllByAttribute(array());

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_button = "Thêm mới";
        $this->tpl_title_list = '';
        $this->dialog = false;
        $this->id = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $this->dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'name'));
        
        $ojbDanhMuc = new DanhMuc();
        if ($this->isPost()) {
        	$this->id = $this->dataPost['itemID'];
        	
        	if ($this->dataPost['mode'] != '') {
        		// Update database
        		if ($this->dataPost['type'] == 'edit' && $this->id != '') {
        			$ojbDanhMuc->setAttribute('name', $this->dataPost['name']);
	        		$ojbDanhMuc->update(array('id' => $this->id));
	        		$this->dialog = true;
        		}
        		
        		// Insert Database
        		if ($this->dataPost['type'] == 'edit' && $this->id == '') {
        			$ojbDanhMuc->setAttribute('seq', $ojbDanhMuc->getMinMax(array(), true) + 1);
		        	$ojbDanhMuc->setAttribute('name', $this->dataPost['name']);
		        	$ojbDanhMuc->insert();
		        	$this->dialog = true;

        		}
        	} else {
        		$this->tpl_button = "Thay đổi";
	        	$this->dataPost = $ojbDanhMuc->getByPrimaryKey($this->id);
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

